<?php

use App\Util\Helper;
use Plugins\AdminBasic\Enums\BasicSuperConfigEnumType;
use Plugins\AdminBasic\Http\Admin\Controller\RouteController;
use Plugins\AdminBasic\Http\Admin\Controller\SuperConfigController;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\JsIf;
use Sc\Util\HtmlStructure\Html\Js\JsVar;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

Html::create('超级配置列表');

$table = Table::create(Helper::route()->to([SuperConfigController::class, 'listsData'])->url(), 'super_config');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('配置的名称', 'name'),
    Table\Column::normal('配置的签名', 'sign')->addSearch('like'),
    Table\Column::normal('配置的类型', 'type')->showTags(BasicSuperConfigEnumType::tagsMapping())->addSearch('in'),
    Table\Column::normal('创建时间', 'create_time'),
);

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setConfig(['width' => '1100px'])->setUrl( Helper::route()->to([SuperConfigController::class, 'create']))
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑')->setConfig(['width' => '1100px'])->setUrl( Helper::route()->to([SuperConfigController::class, 'edit']), ['id' => '@id'])
);
//'@primary.Setting.配置'
$table->setRowEvent('@primary.Setting.配置',
    JsCode::make(
        JsVar::def('width'),
        JsIf::when("row.type === 4 && window.innerWidth > 800")
            ->then(
                JsVar::assign('width', '90%')
            )->else(
                JsIf::when('row.type === 2 || row.type === 3')->then(
                    JsVar::assign('width', '600px'),
                )->else(
                    JsVar::assign('width', '1100px')
                )
            ),
        Table\EventHandler::window('配置')->setConfig(['width' => \Sc\Util\HtmlStructure\Html\Js\Grammar::mark("width")])
            ->setUrl( Helper::route()->to([SuperConfigController::class, 'baseConfig'], ['id' => '@id'])),
    )
);

$table->setRowEvent('@primary.Plus.菜单', function (){
    $superConfigAddMenu = 1;
    $temporary = Temporary::create('menu')->setContent(include __DIR__ . '/../Route/edit.sc.php');
    return Table\EventHandler::window('加菜单')->setConfig(['width' => '600px'])
        ->setRowData([
            'title' => '@row.name',
            'route' => Js::grammar(sprintf( '`%s?_tab=1&id=${row.id}`', Helper::route()->getRoute([SuperConfigController::class, 'baseConfig']))),
        ])->setComponent($temporary);
});

if (Helper::isDev()) {
    $table->setHeaderEvent("@danger.delete.删除",
        Table\EventHandler::post(
            url: Helper::route()->to([SuperConfigController::class, 'destroy']),
            data: ['ids' => '@selection.map(v => v.id)'],
            successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.super_configGetData()")
        )->confirmMessage('确认删除选中数据吗？')
            ->addLoading()
    );
}

$table->addSearch(
    \Sc\Util\HtmlStructure\Form\FormItem::text('name')->placeholder('名称'),
    'like_right'
);

return $table->render();
